<?php

/**
 * Created by Sven.
 * Date: 2016/12/23
 * Time: 15:48
 *  操作cms_nav表的数据库实体类
 */
class NavModel extends Model {
    protected  $_table = 'cms_nav'; // 该模型对应的表名

    private $id; // 编号
    private $nav_name; // 导航名称
    private $nav_info; // 导航描述
    private $pid = 0; // 上一级id
    private $sort; // 导航排序

    private $limit;

    // 拦截器
    public function __get( $name ) {
        return $this->$name;
    }

    public function __set( $name, $value ) {
        // 开启输入验证
        $this->$name = Tool::mysqlString($value);
    }

    public function addNav() {
        /*
         // 排序和id一样
        $_sql = "INSERT 
                                  INTO 
                                  cms_nav(
                                    nav_name,
                                    nav_info,
                                    pid,
                                    sort
                                  ) 
                                  VALUES (
                                      '$this->nav_name',
                                      '$this->nav_info',
                                      0,
                                      ".($this->nextAutoIncrementId('cms_nav'))."
                                  );";
        */

        // 排序是自己定义的，默认0
        $_sql = "INSERT 
                                  INTO 
                                  cms_nav(
                                    nav_name,
                                    nav_info,
                                    pid,
                                    sort
                                  ) 
                                  VALUES (
                                      '$this->nav_name',
                                      '$this->nav_info',
                                      0,
                                      ".($this->sort)."
                                  );";

        return parent::execute( $_sql );
    }

    public function deleteNav() {
        $_sql = "DELETE 
                                  FROM cms_nav
                                  
                                  WHERE  id = '$this->id'
                                  LIMIT 1;
                        ";
        return parent::execute( $_sql );
    }

    public function updateNav() {
        $_sql = "UPDATE 
                                    cms_nav 
                                    set 
                                        nav_name='$this->nav_name', 
                                        nav_info='$this->nav_info'
                                         
                                    where
                                      id='$this->id' 
                                    LIMIT 1; ";


        return parent::execute( $_sql );
    }

    public function getSingleNav() {
        $_sql = "SELECT 
                                n1.id, 
                                n1.nav_name, 
                                n1.nav_info,
                                n2.id as n2id,
                                n2.nav_name as n2nav_name
                            FROM 
                              cms_nav n1
                            LEFT JOIN 
                              cms_nav n2
                              ON n1.pid = n2.id
                            WHERE
                              n1.id = '$this->id'
                              OR 
                              n1.nav_name = '$this->nav_name';
                             ";

        return parent::one( $_sql );
    }

    public function getNavCountByName() {
        $_sql = "SELECT 
                                    COUNT(1)  as count
                                    
                                     FROM 
                                                   cms_nav
                                      WHERE  nav_name = '$this->nav_name'  ";

        return parent::one( $_sql )->count;
    }

    public function getAllNavs() {
        $_sql = "SELECT 
                                id, 
                                nav_name,
                                nav_info,
                                sort
                            FROM 
                              cms_nav
                            WHERE
                              pid =  '$this->pid'
                            ORDER BY 
                              sort ASC
                             $this->limit; 
                             ";

        return parent::query($_sql);
    }


    // 子导航相关

    public function getChildTotalCount(){
        return parent::getCount($this->_table, 'pid', $this->pid);
    }

    public function getAllChildNavs(){

        $_sql = "SELECT 
                                id, 
                                nav_name,
                                nav_info,
                                sort
                            FROM 
                              cms_nav
                            WHERE
                              pid=$this->pid
                            ORDER BY 
                              sort ASC
                             $this->limit; 
                             ";

        return parent::query($_sql);
    }

    public function addChildNav(){
        $_sql = "INSERT 
                                  INTO 
                                  cms_nav(
                                    nav_name,
                                    nav_info,
                                    pid,
                                    sort
                                  ) 
                                  VALUES (
                                      '$this->nav_name',
                                      '$this->nav_info',
                                      $this->pid,
                                      ".($this->nextAutoIncrementId('cms_nav'))."
                                  );";

        return parent::execute($_sql);
    }

    public function getNavCountById(){
        return parent::getCount($this->_table, 'id', $this->id);
    }

    public function getFrontNav(){
        $_sql = "SELECT 
                                    id,
                                    nav_name
                                    FROM 
                                        cms_nav
                                     WHERE 1=1 
                                     LIMIT 0,10; ";

        return parent::query($_sql);
    }

    public function setNavSort(){
        if (!empty($this->sort)){
            $_sqls = array();
            foreach ($this->sort as $_id => $_sort){
                if (! is_numeric($_sort) ) continue;
                $_sqls[] = "UPDATE cms_nav set sort= '$_sort'  WHERE  id='$_id' ";
            }

            $_sql = implode(';',  $_sqls  );
            return parent::multi_query($_sql);
        }

    }

    public function getNavByPid(){
        $_sql = "select id from cms_nav where pid='{$this->pid}'";
        return parent::query($_sql);
    }

    public function getAllSubId(&$_id_array){
        $_id_array[] = $this->id;
        $_sql = "select id from cms_nav where pid = '{$this->id}'";
        $_data = parent::query($_sql);
        foreach ($_data as $_row){
            $this->id =  $_row->id;
            $this->getAllSubId($_id_array);
        }

    }

}